/*
 * 
 */
define(function(require) {
	var comm = require('sdk/server');
	require('sdk/common');

	//微信
	$('#wx').on('click', function() {
		//检查微信是否安装
		if (window.uexWeiXin) {
			uexWeiXin.isWXAppInstalled();
		} else {
			alert('未检测到uexWeiXin插件');
		}
	});

	//主题
	$('#theme').on('click', function() {
		appcan.applyTheme({
			primary: "#"+parseInt(Math.random()*1e6)
		});
	});
	//支付成功
	$('#paydone').on('click', function() {
		appcan.openView(null, 'shop', 'paydone');
	});

	//openweb
	$('#openweb').on('click', function() {
		appcan.openView(null, 'http://baidu.com');
	});

	//3des加解密
	$('#crypto').on('click', function() {
		var CryptoJS = appcan.crypto.core;
		var CryptoCfg = {
			mode: CryptoJS.mode.ECB,
			padding: CryptoJS.pad.Pkcs7
		};
		var keyHex = CryptoJS.enc.Utf8.parse('abcd1234');
		//加密
		var encrypted = CryptoJS.TripleDES.encrypt('hello', keyHex, CryptoCfg);
		var encryptedStr = encrypted.ciphertext.toString();
		alert('加密结果：' + encryptedStr);
		//解密
		var encryptedHexStr = CryptoJS.enc.Hex.parse(encryptedStr);
		var encryptedBase64Str = CryptoJS.enc.Base64.stringify(encryptedHexStr);
		var decryptedData = CryptoJS.TripleDES.decrypt(encryptedBase64Str, keyHex, CryptoCfg);
		var decryptedStr = decryptedData.toString(CryptoJS.enc.Utf8);
		alert('解密结果：' + decryptedStr);
	});


	appcan.ready(function() {
		//增量更新测试
		var getZipPack = function(remoteZip, unZipName, callback) {
			var download = require('download'),
				extPath = "wgt://myspace/ext/",
				cacheZip = "";
			uexZip.cbUnZip = function(opCode, dataType, data) {
				if (data == 0) {
					appcan.window.openToast('解压完成！', '2000', 8);
					appcan.file.remove({
						filePath: cacheZip,
						callback: function(err, data, dataType, optId) {
							if (err) {
								return;
							}
							if (data == 0) {
								appcan.window.openToast('已删除压缩包', '2000', 8);
							}
						}
					});
					appcan.file.getRealPath(extPath, function(err, data, dataType, optId) {
						if (err) {
							return appcan.window.openToast('压缩包路径解析错误！', '3000', 8);
						}
						var realPath = 'file://' + data + unZipName + '/temp.html';
						typeof(callback) === 'function' && callback(realPath);
					});
				} else {
					appcan.window.openToast('解压失败！', '3000', 8);
				}
			};
			download(remoteZip, {
				name: unZipName + '.zip',
				success: function(path) {
					cacheZip = path;
					uexZip.unzip(path, extPath + unZipName);
				}
			});
		};

		var newPack = {
			url: 'http://7xnt8z.com1.z0.glb.clouddn.com/notice2.zip?_='+Math.random()*1e9,
			name: 'notice2',
			localPath: ''
		};
		$('#downloadZip').on('click', function() {
			getZipPack(newPack.url, newPack.name, function(res) {
				newPack.localPath = res;
			});
		});
		$('#testZip').on('click', function() {
			appcan.ls.val('notice', 'hello !');
			if (newPack.localPath) {
				appcan.openView({
					fullPath: true
				}, newPack.localPath);
			} else {
				alert('页面未下载')
			}
		});

		//微信支付
		uexWeiXin.registerApp('wx5da948b8f98c1b12');
		uexWeiXin.cbIsWXAppInstalled = function(opCode, dataType, data) {
			if (data == "1") {
				alert('微信未安装');
			} else {
				uexWeiXin.isWXAppSupportApi();
			}
		};
		uexWeiXin.cbIsWXAppSupportApi = function() {
			if (data == "1") {
				alert('微信API不支持');
			} else {
				uexWeiXin.isSupportPay()
			}
		};
		uexWeiXin.cbIsSupportPay = function(opId, dataType, data) {
				if (data == "1") {
					alert('不支持微信支付');
				} else {
					alert('该设备支持微信支付');
				}
			}
			// uexWeiXin.cbGetPrepayId = function(data){
			//     alert(data);
			// }
	});
});